import { myRequest } from '@/util/api.js'


export const payOptions = [
					{
					    "title": "储值",
					    "icon": "/static/img/balancepay.png",
					    "id": "balance",
					    "key": '3'
					},
				
					{ 
					    "title": "赠送",
					    "icon": "/static/img/givepay.png",
					    "id": "give",
					    "key": '13'
					},
				 
					{
					    "title": "现金",
					    "icon": "/static/img/cashpay.png",
					    "id": "cash",
					    "key": '1'
					},
				
					{
					    "title": "微信",
					    "icon": "/static/img/wxpay.png",
					    "id": "wx",
					    "key": '5'
					},
				
					{
					    "title": "支付宝",
					    "icon": "/static/img/alipay.png",
					    "id": "alipay",
					    "key": '4'
					},
				
					{
					    "title": "扫码",
					    // "icon": "/static/img/scancodepay.png",
					    "id": "scan_code",
					    "key": '8'
					},
					{
					    "title": "美团",
					    // "icon": "/static/img/mtpay.png",
					    "id": "mt",
					    "key": '9'
					},
					{
					    "title": "其他",
					    // "icon": "/static/img/elsepay.png",
					    "id": "other",
					    "key": '20'
					},
				 ];
//微信请求

export function OrderBean(memberId, orderType, orderId) { 
		this.orderId = -1;
		if(orderId && orderId > 0) {
			this.orderId = orderId;
		}
		this.orderType = -1 ; //订单类型 
		if(orderType > -1) {
			this.orderType = orderType;
		}
		this.memberId = memberId || null;
		this.orderStatus = 110;             //待支付状态
		this.totalPrice = 0;  ///应收
		this.couponInfoId = -1;
		this.activityId = -1;
		this.orderSource = "outline";
		this.payInfos = new Array();       //支付方式及支付金额
		this.newMember = false;  ////是否为新客户
		if(!memberId || memberId <=0 ) {//没消费过的会员开卡算新客
			this.newMember = true;
		} else if(memberId && memberId > 0) {//没消费过的会员开卡算新客
			this.newMember = true;
		};
		this.orderDate = "";
		this.projectCarts = [];  /////项目购物车
		this.productCarts = [];  ////产品购物车
		this.memberCard = {};   /////主卡
		this.orderRemark = "";
		this.orderArrears = 0;   ///订单欠款
		this.deducableAmount = 0;  ////可提成金额
		this.staffNewMembers = [];
		this.orderStaffRecords = [];  ///员工提成
		this.newClientType = null;  ////新客户类型
		this.payments = "";
		this.saveCombin = false;  ///是否为主卡
		this.actualAmount = 0;  ////实付
		this.orderRecharge = {};
		this.combinId = null;//卡类型卡项id
		this.cardId = null;
		this.ofId = null;
		this.upgradeCardContent = ""
		this.categoryId = ""
		this.fission_mobile = ""
		this.fission_name = ""
		this.fissionactivityId = -1
		
	 	this.addProjectCart = function(manual, price, count, goods_id, projectStaffRecords, surplus, consume, goods_type,multi,multiProjects) {
	 		this.projectCarts.push({manual:manual,price:price,count:count,goods_id:goods_id,projectStaffRecords:projectStaffRecords,surplus:surplus,consume:consume,goods_type:goods_type,multi:multi,multiProjects:multiProjects});
	 	};
	 	this.addProductCart = function(goods_id, goodsSpecs_id, count, price,surplus, goods_type) {
	 		this.productCarts.push({goods_id: goods_id,goodsSpecs_id:goodsSpecs_id, count: count, price: price,surplus:surplus,goods_type:goods_type});
	 	};
	 	this.addStaffRecords = function(manual, project_unit, dept_type, extractRate, projectCount, staff_id, staff_name, salesAmount, consume) {
	 		this.orderStaffRecords.push({manual:manual,project_unit:project_unit,dept_type:dept_type,extractRate:extractRate,projectCount:projectCount,staff_id:staff_id,staff_name:staff_name,salesAmount:salesAmount,consume:consume});
	 	};
	 	this.addstaffNewMembers = function(staff_id, dept_type, newMemberCount) {
	 		if(newMemberCount) {
	 			existed = false;//
	 			for (var i = 0; i < this.staffNewMembers.length; i++) {
	 				var row = this.staffNewMembers[i];
	 				if(row.staff_id == staff_id) {
	 					existed = true;
	 				} 
	 			}
	 			if(!existed) {
	 				this.staffNewMembers.push({staff_id: staff_id,dept_type:dept_type, newMemberCount: newMemberCount});
	 			}
	 		} 
	 		
	 	};
	 	this.addPayInfos = function(paytype, amount, staff_id) {
	 		this.payInfos.push({paytype: paytype, amount: amount,staff_id: staff_id});
	 	};
	 	/**是否使用微信支付**/
	 	this.useWXPay = function() {
	 		var wxpi = null;
	 		jQuery.each(this.payInfos, function (i) {
	 			if(this.paytype == 5) {
	 				wxpi = this;
	 			}
	 		})
	 		return wxpi != null;
	 	};
		this.toJSONStr = function() {
			return JSON.stringify(this.getData()); 
		};
		this.getData = function() {
			return {
				orderId: this.orderId,
				orderType: this.orderType,storeId: this.storeId,
				memberId: this.memberId, totalPrice: this.totalPrice, 
				couponInfoId: this.couponInfoId,
				activityId: this.activityId,
				orderStatus: this.orderStatus, 
				orderSource: this.orderSource,
				payInfos: this.payInfos,
				newMember: this.newMember,
				orderDate:this.orderDate,
				projectCarts:this.projectCarts,
				memberCard:this.memberCard,
				orderRemark:this.orderRemark,
				orderArrears:this.orderArrears,
				deducableAmount:this.deducableAmount,
				staffNewMembers:this.staffNewMembers,
				orderStaffRecords:this.orderStaffRecords,
				newClientType:this.newClientType,
				payments:this.payments,
				productCarts:this.productCarts,
				saveCombin:this.saveCombin,
				actualAmount:this.actualAmount,
				orderRecharge:this.orderRecharge,
				cardId:this.cardId,
				ofId: this.ofId,
				upgradeCardContent: this.upgradeCardContent,
				categoryId: this.categoryId
				}; 
		};
		this.calcCartTotalPrice= function(type){///计算总价
			var that = this;
		    if (that.orderType == 0) {//开项目卡
// 				var combin_price = that.memberCard.totalPrice;//主卡价格
// 				if (combin_price && combin_price != "") {  ///项目卡
// 					return combin_price;//有主卡时直接返回主卡价格
// 				}
			}
			var projGoodsTotalPrice = 0;
			var prodGoodsTotalPrice = 0;
			for(var i=0; i<this.projectCarts.length; i++) {
				if(!this.projectCarts[i].cardId) {
					projGoodsTotalPrice+=(this.projectCarts[i]).totalPrice;
				}
			}
			for(var i=0; i<this.productCarts.length; i++) {
				if(!this.productCarts[i].cardId) {
					prodGoodsTotalPrice+=(this.productCarts[i]).totalPrice;
				}
			}
			if(typeof type == 'undefined') {
				var total = projGoodsTotalPrice + prodGoodsTotalPrice;
				if (this.orderType == 0) {  /////客户开卡
					if(this.orderRecharge.price > 0) {
						total += this.orderRecharge.price
					}
				} else if (this.orderType == 3) { ///还欠款
					total = $("#mustPay").val() || 0
				} else if (this.orderType == 5) { //退卡
					total += parseFloat($("#card_fill_form input[name=orderAmount]").val() || 0)
					total -= parseFloat($("#card_fill_form input[name=cardArrears]").val() || 0)
					total -= parseFloat($("#card_fill_form input[name=usedAmount]").val() || 0)
					total *= -1
				} else if (this.orderType == 4) {  //转卡升级
					if (this.orderRecharge.price) {
						total += this.orderRecharge.price
					}
					if (this.orderRecharge.giveAmount) {
						total += this.orderRecharge.giveAmount
					}
				} 
				if (this.couponInfoId != -1 && total) { ///收银使用优惠券
					var params = {edit_cf_id:(orderData && orderData.couponInfoId || 0),cf_id: this.couponInfoId,totalPrice:total}
					FT.ajaxSynJson(ctx+'/seller/memberCouponCheck.htm',params,
						 function(data) {
						 if (data.errorCode == '0') {
							  var obj = data.retObj;
							  if (obj.isUse) {
								  $('#cashierCoupon').val(obj.useInfo.coupon_name)
								  that.coupon = obj.useInfo
							  } else {
									FT.warning(obj.useInfo)
									FT.ui.getCmp('cashierCoupon') && FT.ui.getCmp('cashierCoupon').setVal('')
									that.couponInfoId = -1
								}
						  } else {
							  FT.warning(data.errorMsg);
							  FT.ui.getCmp('cashierCoupon') && FT.ui.getCmp('cashierCoupon').setVal('')
							  that.couponInfoId = -1
						  }
					 })
					if (this.coupon) {
						if(this.coupon.coupon_type==1 || this.coupon.coupon_type==3){//代金券 或者积客券
							total = parseFloat(total)-parseFloat(this.coupon.coupon_amount);
							if (parseFloat(total) < parseFloat(this.coupon.coupon_amount)) {  ////如果优惠券金额大于应付金额
								total = 0
							}
						}else{//折扣券
							total = parseFloat(total)*parseFloat(this.coupon.coupon_amount)
						}
					}
				}
				this.totalPrice = total;
				return total;
			} else if( type == 1) {
				return projGoodsTotalPrice;
			} else if( type == 2) {
				return prodGoodsTotalPrice;
			}
		};
		this.getGoodsRecordNum= function(){//购物车记录数
			var projGoodscount = this.projectCarts.length;
			var prodGoodscount = this.productCarts.length;
			return projGoodscount + prodGoodscount;
		};
		this.getGoodsCount= function(type){
				var projGoodscount = 0;
				var prodGoodscount = 0;
				for (var i = 0; i < this.projectCarts.length; i++) {
					projGoodscount += this.projectCarts[i].count;
				}
				for (var i = 0; i < this.productCarts.length; i++) {
					prodGoodscount += this.productCarts[i].count;
				}
				if(typeof type == 'undefined') {
					return projGoodscount + prodGoodscount;
				} else if( type == 1) {
					return projGoodscount;
				} else if( type == 2) {
					return prodGoodscount;
				}
		};
		this.getPrjConsumeTotal = function(){
			 var consume = 0;//店长取购物车的消耗累计
	    	 jQuery.each(this.projectCarts, function(i, prj){ 
	    		    consume += prj.consume * prj.count; 
			 }) 
			return consume;
		};
		this.saleStaff = {
			'adviser' : {},
			'operator' : {},
			 getAdviserCount : function(){
				 var count = 0;
				 if(this.adviser) {
		    		 for(var staffid in this.adviser) {
		    			 count++;
		    		 }
				 }
				 return count;
			 },
			 getOperatorCount : function(){
				 var count = 0;
				 if(this.operator) {
		    		 for(var staffid in this.operator) {
		    			 count++;
		    		 }
				 }
				 return count; 
			 }
		}
	};
 
export function goodsClassTreeList(data) {
	return myRequest('goods/goodsClassTreeList',data,'GET')
}
 
export function goodsList(data) {
	return myRequest('goods/goodsList',data,'GET')
}
 
export function netGoodsDetail(data) {
	return myRequest('goods/goodsDetail',data,'GET')
}

export function netGoodsInfoDetail(data) {
	return myRequest('goods/goodsInfoDetail',data,'GET')
}

/**
 * 获取推荐产品
 * 
 */
export function getProductHot(page, limit) {
	
    return myRequest('goods/productHot',{
		page: page === undefined ? 1 : page,
		limit: limit === undefined ? 4 : limit
	},'GET'); 
}

export function combineList(data) {
	return myRequest('goods/combineList',data,'GET')
}
export function staffBonusSetting(data) {
	return myRequest('staff/staffBonusSetting',data,'GET')
}


export function netSaveOrder(data) {
	return myRequest('order/save',data)
}
 
export function getMemberDiscountBean(data) {
return myRequest('order/getMemberDiscountBean',data, 'GET')
}

//客户列表
export function netOrderList(data) {
	return myRequest('order/getList',data, 'GET')
}
 
export function netOrderAudit(data) {
	return myRequest('order/audit',data, 'GET')
}
export function netOrderSignSave(data) {
	return myRequest('order/signSave',data)
}
export function netOrderModifyAchieve(data) {
	return myRequest('order/modifyAchieve',data, 'GET')
}
//客户列表
export function netOrderDetail(data) {
	return myRequest('order/detail',data, 'GET')
}
 
export function netGetSaleSumData(data) {
	return myRequest('order/getSaleSumData',data, 'GET')
}

export function netGetConsumeSumData(data) {
	return myRequest('order/getConsumeSumData',data, 'GET')
}

//客户列表
export function netMemberCardList(data) {
	return myRequest('customer/getMemberCardList',data, 'GET')
}

//创建 跟进 记录
export function netGetMembercard(data) {
	return myRequest('customer/getMembercard',data, 'GET')
}
 
 

//添加收支
export function netDailyInExSave(data) {
	return myRequest('dailyInEx/save',data)
}
 
//添加收支
export function netDailyInExList(data) {
	return myRequest('dailyInEx/list',data, 'GET')
}
 
//shanch 收支
export function netDailyInExDelete(data) {
	return myRequest('dailyInEx/delete',data, 'GET')
}
 
